import { createRouter, createWebHashHistory} from "vue-router";
import UserLogin from "@/components/UserLogin.vue";
import MainPage from "@/components/frame/MainPage.vue";
import {useUserStore} from "../store/userStore.js";
import ForgotPassword from "@/components/frame/ForgotPassword.vue";

const routes = [
    {
        path: "/login",
        component: UserLogin
    },
    {
        name: "MainPage",
        path: "/",
        Redirect:"/MainPage",
        component: MainPage,

        children: [
            {
                name: 'CheckIn',
                path: '/CheckIn',
                component:function (){
                    return import('@/views/CustomerManage/CheckIn.vue')
                }
            },
            {
                name: 'CheckOut',
                path: '/CheckOut',
                component:function (){
                    return import('@/views/CustomerManage/CheckOut.vue')
                }
            },
            {
                name: 'OutRecords',
                path: '/OutRecords',
                component:function (){
                    return import('@/views/CustomerManage/OutRecords.vue')
                }
            },
            {
                name: 'UserMassage',
                path: '/UserMassage',
                component:function (){
                    return import('@/views/CustomerManage/UserMassage.vue')
                }
            } ,
            {
                name: 'food',
                path: '/food',
                component:function (){
                    return import('@/views/FoodManage/foodindex.vue')
                }
            },
            {
                name: 'foodlove',
                path: '/foodlove',
                component:function (){
                    return import('@/views/FoodManage/foodlove.vue')
                }
            },
            {
                name: 'bedManage',
                path: '/bedManage',
                component: function(){
                    return import('@/views/BedManage/bedManage.vue')
                }
            },
            {
                name: 'bedMap',
                path: '/bedMap',
                component: function(){
                    return import('@/views/BedManage/bedMap.vue')
                }
            },
            {
                name: 'NurseItem',
                path: '/NurseItem',
                component: function(){
                    return import('@/views/NurseManage/NurseItem.vue')
                }
            },
            {
                name: 'NurseLevel',
                path: '/NurseLevel',
                component: function(){
                    return import('@/views/NurseManage/NurseLevel.vue')
                }
            },
            {
                name: 'NurseRecords',
                path: '/NurseRecords',
                component: function(){
                    return import('@/views/NurseManage/NurseRecords.vue')
                }
            },
            {
                name: 'NurseSetting',
                path: '/NurseSetting',
                component: function(){
                    return import('@/views/NurseManage/NurseSetting.vue')
                }
            },
            {
                name: 'UserRegister',
                path: '/UserRegister',
                component: function(){
                    return import('@/views/UserSetting/UserRegister.vue')
                }
            },{
                name: 'UserManage',
                path: '/UserManage',
                component: function(){
                    return import('@/views/UserSetting/UserManage.vue')
                }
            },{
                name: 'ServiceCare',
                path: '/ServiceCare',
                component: function(){
                    return import('@/views/HealthManage/ServiceCare.vue')
                }
            },{
                name: 'ServiceCustomerSetting',
                path: '/ServiceCustomerSetting',
                component: function(){
                    return import('@/views/HealthManage/ServiceCustomerSetting.vue')
                }
            },
            {
                path: '/EditPim',
                name: 'EditPim',
                component: function(){
                    return import('@/components/frame/EditPim')}
            },
        ],
    }, 

]
const router = createRouter ({
    history:createWebHashHistory(),
    routes
})

const whiteList=['/login']
router.beforeEach(
    (to, from, next)=>{
        const path = to.path
        const userStore = useUserStore();

        if(whiteList.includes(path)){
            console.log("路由守卫：白名单直接跳转");
            next();
            return true;
        }

        if(localStorage.getItem('token')){
            console.log("路由守卫：通过");
            next();
            return true;
        } else {
            console.log("路由守卫：未登录");
            next("/login")
            return false;
        }
    }
)

export default router;